<template>
	<el-dialog :title="dialogTitle" :visible.sync="visible" width="586px" top="0" append-to-body>
		<el-form v-bind="formConfig" :model="formData" :rules="formRules" label-width="80px" v-loading="loading">
			<el-row :gutter="16">
				<el-col :span="!child ? 18 : 24">
					<el-form-item label="上级导航" prop="pid">
						<sw-search-tree-select v-model="formData.pid" v-bind="treeSelectConfig" :tree-data="treeListData" :disabled="!isDisabled" @change="handleTreeSelect" />
					</el-form-item>
				</el-col>
				<el-col :span="6" v-if="!child">
					<el-form-item label="是否外链" prop="isOutsite">
						<el-switch v-model="formData.isOutsite" active-value="1" inactive-value="2" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="导航名称" prop="name">
						<el-input v-model="formData.name" maxlength="6"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="formData.isOutsite === '1'">
					<el-form-item label="导航链接" prop="value">
						<el-input v-model="formData.value"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="14">
					<el-form-item label="是否显示" prop="isShow">
						<el-radio-group v-model="formData.isShow" v-removeAriaHidden:radio>
							<el-radio label="1">显示</el-radio>
							<el-radio label="2">隐藏</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="10">
					<el-form-item label="排序" prop="orderBy" label-width="48px">
						<el-input-number v-model="formData.orderBy" controls-position="right" :min="1" style="width: 100%" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="导航描述">
						<el-input type="textarea" v-model="formData.des"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="btnLoading" @click="handleSubmitForm()">{{ formTypeName }}</el-button>
		</template>
	</el-dialog>
</template>

<script>
import lodash from 'lodash';
import formMixins from '@/mixins/formMixins';
import { columnDetail, addedColumn, updateColumn } from '@/api/website/sysWebColumn';
export default {
	mixins: [formMixins],
	props: {
		topId: { type: String, default: '' },
		list: { type: Array, default: () => [] }
	},
	data() {
		return {
			treeListData: [],
			formName: '顶部导航',
			formRules: {
				name: [{ required: true, message: '请输入导航名称', trigger: 'blur' }],
				pid: [{ required: true, message: '请选择上级导航', trigger: 'change' }],
				value: [{ required: true, message: '请输入导航链接', trigger: 'blur' }],
				isShow: [{ required: true, message: '请选择显示状态', trigger: 'change' }],
				isOutsite: [{ required: true, message: '请选择是否外链', trigger: 'change' }]
			},
			treeSelectConfig: {
				placeholder: '选择顶部导航',
				style: { width: '100%' },
				defaultProps: { label: 'name', children: 'children' }
			}
		};
	},
	computed: {
		detailData() {
			return eval(columnDetail);
		},
		callFunction() {
			return eval(!this.addOrEdit && !this.child ? updateColumn : addedColumn);
		},
		isDisabled() {
			return !this.addOrEdit && !this.child;
		},
		submitParams() {
			let datas = { ...this.formData };
			if (datas['isOutsite'] === '2') {
				datas['value'] = '';
			}
			datas['classType'] = '101';
			return datas;
		}
	},
	methods: {
		formatFormParams(data) {
			if (!this.topId) return false;
			let list = this.$treeToFlat(this.list);
			let listData = lodash.filter(list, { isOutsite: '2' });
			let children = this.$flatToTree(listData, 'pid', this.topId);
			this.treeListData = [{ id: this.topId, name: '顶部导航', children }];
			if (!this.addOrEdit && !this.child) {
				this.$removeNodeInTree(children, this.dataId);
				this.$set(this.formData, 'classLevel', data.classLevel);
				if (data.isOutsite === '2') delete this.treeListData[0].children;
			} else if (this.child) {
				this.$set(this.formData, 'isOutsite', '1');
			} else {
				this.$set(this.formData, 'pid', this.topId);
				this.$set(this.formData, 'classLevel', '2');
				this.$set(this.formData, 'isOutsite', '2');
			}
		},

		handleTreeSelect(data) {
			if (this.topId === data.id) return false;
			if (data.isOutsite === '2') {
				console.log('123123');
			}
			let level = parseInt(data.classLevel || '2');
			this.$set(this.formData, 'classLevel', level + 1);
			console.log(this.formData);
		}
	}
};
</script>
